import logging
from fastapi import APIRouter, status, Depends
from sqlalchemy.ext.asyncio import AsyncSession
from fastapi.security.api_key import APIKey

from price_analysis.core.models import db_helper, IncomingSO
from price_analysis.auth import auth

from . import crud
from . import schemas
from ..incoming_so.crud import get_all_incoming_so
from price_analysis.utils.recognize import recognize_main


logger = logging.getLogger("main")

router = APIRouter(tags=["recognize_so"])


@router.post(
    "/",
    status_code=status.HTTP_200_OK,
)
async def recognize_so(
    command: schemas.RecognizeSoCommand,
    api_key: APIKey = Depends(auth.get_api_key),
    session: AsyncSession = Depends(db_helper.get_async_session),
):
    logger.info(f"получена команда {command.command} {command.data}")

    match command.command:
        case "START":
            file_list: list[IncomingSO] = await get_all_incoming_so(session=session)
            await recognize_main(file_list)

    return {"result": "success"}


@router.get("/")
async def get_recognized_data(
    api_key: APIKey = Depends(auth.get_api_key),
):
    return await crud.get_all_recognized_data()
